<template>
	<view class="checkVersion">
		<view class="prompt">
			当前版本<text>{{currentVersion}}</text>，最新版本<text>{{latestVersion}}</text>
		</view>
		<view class="update-button" v-if="currentVersion != latestVersion">
			<view class="_prompt">有新版本更新，请更新...</view>
			<view class="_button" @click="clickUpdateButton">{{promptText}}</view>
			<view class="progress-bar">
				<view class="progress-bar-text">下载进度</view>
				<view class="progress-bar-box">
					<view :style="'width:'+progressBar+'%;'"></view>
				</view>
				<view class="progress-bar-box-text">{{progressBar}}%</view>
			</view>
		</view>
		<view class="prompt-title">更新日志：</view>
		<view class="log">
			<view v-html="log"></view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				currentVersion:"",//当前版本
				latestVersion:"",//最新版本
				log:"",//更新日志
				url:"",
				promptText:"更新",
				progressBar:"0"
			}
		},
		onLoad() {
			uni.request({
				url:this.$_configFile() + "checkVersion.json",
				method:"GET",
				success: (res) => {
					this.currentVersion = plus.runtime.version;
					this.latestVersion = res.data.latestVersion;
					this.log = res.data.log;
					this.url = res.data.downloadAddress;
				}
			})
		},
		methods: {
			clickUpdateButton(){
				if(this.promptText == "下载中"){
					uni.showToast({
						title: '下载中，请不要重复点击',
						icon: 'none',
						duration: 1500
					});
					return
				}
				this.promptText = "下载中";
				const downloadTask = uni.downloadFile({
					url: this.url,
					success: (res) => {
						if (res.statusCode === 200) {
							let url = res.tempFilePath;
							uni.openDocument({
								filePath: url,
								success: (_res) => {}
							});
						}
					}
				});
				downloadTask.onProgressUpdate((res) => {
					this.progressBar = res.progress;
				});
			}
		}
	}
</script>

<style lang="scss">
.checkVersion{
	.prompt{
		background: #FFF;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		line-height: 28rpx;
		padding: 25rpx 0;
		text{
			padding-left: 5rpx;
			color: $red;
		}
	}
	.update-button{
		background: #FFF;
		margin-top: 25rpx;
		padding-bottom: 25rpx;
		._prompt{
			font-size: 22rpx;
			line-height: 22rpx;
			padding: 20rpx;
		}
		._button{
			width: 200rpx;
			height: 60rpx;
			background: $theme;
			color: #FFF;
			font-size: 22rpx;
			line-height: 22rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			margin: auto;
			border-radius: 10rpx;
		}
		.progress-bar{
			display: flex;
			align-items: center;
			margin: 25rpx;
			.progress-bar-text{
				flex-shrink: 0;
				color: $placeholder;
				line-height: 22rpx;
				font-size: 22rpx;
			}
			.progress-bar-box{
				flex: 1;
				margin: 0 15rpx;
				display: flex;
				align-items: center;
				border: 1px solid $border;
				border-radius: 100rpx;
				overflow: hidden;
				view{
					width: 100%;
					background: $theme;
					height: 32rpx;
				}
			}
			.progress-bar-box-text{
				flex-shrink: 0;
				color: $placeholder;
				line-height: 22rpx;
				font-size: 22rpx;
			}
		}
	}
	.prompt-title{
		font-size: 28rpx;
		line-height: 28rpx;
		padding: 25rpx;
	}
	.log{
		background: #FFF;
		padding:25rpx;
		h2{
			font-size: 32rpx;
			line-height: 32rpx;
			padding-top: 15rpx;
			padding-bottom: 15rpx;
		}
		p{
			font-size: 28rpx;
			line-height: 45rpx;
			color: $placeholder;
		}
	}
}
</style>
